//
//  AHBuildable.swift
//  AttributedStringHelper
//
//  Created by JY_NEW on 2021/11/1.
//

import Foundation

/// 定义所有可能出现的类型
public indirect enum AHBuildItem {
    case string(String, [NSAttributedString.Key: Any]?)
    case attributedString(NSAttributedString)
}
public protocol AHBuildable {
    /// 构建项
    func __attributedStringHelperPerformBuildItem() -> AHBuildItem?
    func __attributedStringHelperPerformBuildItems() -> [AHBuildItem]?
}
extension AHBuildable {
    public func __attributedStringHelperPerformBuildItems() -> [AHBuildItem]? {
        return nil
    }
}

// MARK:  空处理
public struct AHBuildNone: AHBuildable {
    public func __attributedStringHelperPerformBuildItem() -> AHBuildItem? {
        return nil
    }
}

// MARK:  数组处理
public struct AHBuildArray: AHBuildable {
    var items: [AHBuildable]
    public func __attributedStringHelperPerformBuildItem() -> AHBuildItem? {
        return nil
    }
    public func __attributedStringHelperPerformBuildItems() -> [AHBuildItem]? {
        var result: [AHBuildItem] = []
        for item in items {
            if let array = item.__attributedStringHelperPerformBuildItems() {
                result.append(contentsOf: array)
            }else if let it = item.__attributedStringHelperPerformBuildItem() {
                result.append(it)
            }
        }
        return result.count > 0 ? result : nil
    }
}

// MARK:  单字符串处理

/// 默认可传String
extension String: AHBuildable {
    public func __attributedStringHelperPerformBuildItem() -> AHBuildItem? {
        return .string(self, nil)
    }
}
/// 默认可传String
extension NSAttributedString: AHBuildable {
    public func __attributedStringHelperPerformBuildItem() -> AHBuildItem? {
        return .attributedString(self)
    }
}

/// 默认可传UIImage
extension UIImage: AHBuildable {
    public func __attributedStringHelperPerformBuildItem() -> AHBuildItem? {
        let attachment = NSTextAttachment()
        attachment.image = self
        attachment.bounds = CGRect(origin: .zero, size: self.size)
        return .attributedString(.init(attachment: attachment))
    }
}
extension FileWrapper: AHBuildable {
    public func __attributedStringHelperPerformBuildItem() -> AHBuildItem? {
        let attachment = NSTextAttachment()
        attachment.fileWrapper = self
        return .attributedString(.init(attachment: attachment))
    }
}
extension Optional: AHBuildable where Wrapped: AHBuildable {
    public func __attributedStringHelperPerformBuildItem() -> AHBuildItem? {
        switch self {
        case let .some(value): return value.__attributedStringHelperPerformBuildItem()
        case .none: return nil
        }
    }
}


public struct AHBuildBuffer {
    let string: String
    let settings: [AHSetting]
}
extension String {
    public func attr(_ settings: AHSetting...) -> AHBuildBuffer {
        return .init(string: self, settings: settings)
    }
}

extension AHBuildBuffer: AHBuildable {
    public func __attributedStringHelperPerformBuildItem() -> AHBuildItem? {
        return .string(string, settings.attributes())
    }
}
